﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.MissileArrive
{
    public partial class MissileArriveEdit : System.Web.UI.Page
    {
        public string ItemCode
        {
            set
            {
                ViewState["ItemCode"] = value;
            }
            get
            {
                return ViewState["ItemCode"] != null ? ViewState["ItemCode"].ToString() : string.Empty;
            }
        }

        public string SysNumber
        {
            set
            {
                ViewState["SysNumber"] = value;
            }
            get
            {
                return ViewState["SysNumber"] != null ? ViewState["SysNumber"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserPermissionBll userPermissionBll = new UserPermissionBll();
                if (!userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.MissileArrive))
                {
                    Response.Write("您没有权限使用该功能");
                    Response.End();
                    return;
                }
                this.ItemCode = Request.QueryString["itemCode"];
                this.SysNumber = Request.QueryString["sysnumber"];
                InitControlData();

                if (userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.MissileArriveConfirm))
                {
                    btnConfirm.Visible = true;
                }
                else
                {
                    btnConfirm.Visible = false;
                }

                if (IsConfirmed())
                {
                    if (userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.MissileArriveConfirm))
                    {
                        this.btnOK.Visible = true;
                    }
                    else
                    {
                        this.btnOK.Visible = false;
                    }
                }
            }
        }

        private void InitControlData()
        {
            string sql = @"SELECT * FROM dbo.OSRN WHERE ItemCode = @ItemCode AND SysNumber = @sysNumber";

            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, this.ItemCode);
                database.AddInParameter(dbCommand, "@sysNumber", DbType.String, this.SysNumber);
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        txtItemCode.Text = reader["ItemCode"] != null ? reader["ItemCode"].ToString() : string.Empty;
                        txtItemName.Text = reader["ItemName"] != null ? reader["ItemName"].ToString() : string.Empty;
                        txtDistNumber.Text = reader["DistNumber"] != null ? reader["DistNumber"].ToString() : string.Empty;
                        txtInDate.Text = reader["InDate"] != null ? reader["InDate"].ToString() : string.Empty;
                        txtYccsj.Text = reader["U_yccsj"] != null ? reader["U_yccsj"].ToString() : string.Empty;
	                    txtTdsj.Text = reader["U_tdsj"] != null ? reader["U_tdsj"].ToString() : string.Empty;
	                    txtGfsj.Text = reader["U_gfsj"] != null ? reader["U_gfsj"].ToString() : string.Empty;
	                    txtCchj.Text = reader["U_cchj"] != null ? reader["U_cchj"].ToString() : string.Empty;
	                    txtYfsj.Text = reader["U_yfsj"] != null ? reader["U_yfsj"].ToString() : string.Empty;
	                    txtQfsj.Text = reader["U_qfsj"] != null ? reader["U_qfsj"].ToString() : string.Empty;
                        txtLifeTime.Text = reader["U_LifeTime"] != null ? reader["U_LifeTime"].ToString() : string.Empty;
                        txtFlightTime.Text = reader["u_FlightTime"] != null ? reader["u_FlightTime"].ToString() : string.Empty;
                        txtNote.Text = reader["u_Note"] != null ? reader["u_Note"].ToString() : string.Empty;

                        string level = reader["U_Level"] != null ? reader["U_Level"].ToString() : string.Empty;
                        ListItem listItem = ddlLevel.Items.FindByValue(level);
                        if (listItem != null)
                        {
                            listItem.Selected = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpdateData()
        {
            string sql = @"UPDATE dbo.OSRN 
                            SET U_yccsj = @yccsj
	                            ,U_tdsj = @tdsj
	                            ,U_gfsj = @gfsj
	                            ,U_cchj = @cchj
	                            ,U_yfsj = @yfsj
	                            ,U_qfsj = @qfsj
	                            ,U_Level = @level
	                            ,U_LifeTime = @lifeTime
                                ,u_FlightTime = @flightTime
                                ,u_Note = @note
                            WHERE ItemCode = @itemCode AND SysNumber = @sysNumber";

            try
            {

                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@yccsj", DbType.String, txtYccsj.Text.Trim());
                database.AddInParameter(dbCommand, "@tdsj", DbType.String, txtTdsj.Text.Trim());
                database.AddInParameter(dbCommand, "@gfsj", DbType.String, txtGfsj.Text.Trim());
                database.AddInParameter(dbCommand, "@cchj", DbType.String, txtCchj.Text.Trim());
                database.AddInParameter(dbCommand, "@yfsj", DbType.String, txtYfsj.Text.Trim());
                database.AddInParameter(dbCommand, "@qfsj", DbType.String, txtQfsj.Text.Trim());
                database.AddInParameter(dbCommand, "@level", DbType.String, ddlLevel.SelectedValue);
                database.AddInParameter(dbCommand, "@lifeTime", DbType.String, txtLifeTime.Text.Trim());
                database.AddInParameter(dbCommand, "@itemCode", DbType.String, this.ItemCode);
                database.AddInParameter(dbCommand, "@sysNumber", DbType.String, this.SysNumber);
                database.AddInParameter(dbCommand, "@flightTime", DbType.String, txtFlightTime.Text.Trim());
                database.AddInParameter(dbCommand, "@note", DbType.String, txtNote.Text.Trim()); 
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ConfirmData()
        {
            string sql = @"UPDATE dbo.OSRN 
                            SET u_Confirmed = 1	                            
                            WHERE ItemCode = @itemCode AND SysNumber = @sysNumber";

            try
            {

                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@itemCode", DbType.String, this.ItemCode);
                database.AddInParameter(dbCommand, "@sysNumber", DbType.String, this.SysNumber);

                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }        
        }

        private bool IsConfirmed()
        {
            string sql = @"SELECT u_Confirmed FROM dbo.OSRN WHERE ItemCode = @itemCode AND SysNumber = @sysNumber";

            try
            {

                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@itemCode", DbType.String, this.ItemCode);
                database.AddInParameter(dbCommand, "@sysNumber", DbType.String, this.SysNumber);
                object obj = database.ExecuteScalar(dbCommand);

                if (obj != null && obj != DBNull.Value)
                {
                    bool isConfirmed = false;
                    if (bool.TryParse(obj.ToString(), out isConfirmed))
                    {
                        if (isConfirmed)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateData();
                Response.Redirect("MissileArriveInfo.aspx"); 
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>"); 
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MissileArriveInfo.aspx");
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                ConfirmData();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('确认成功');</script>");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
            }
        }
    }
}